﻿using System;
using System.Diagnostics;
using System.Security;

namespace Jarf
{
	/// <summary>
	/// Provides an easy way of writing to the application event log.
	/// </summary>
	public class Log
	{
		/// <summary>
		/// Source of the events (displayed in a column in the event viewer).
		/// </summary>
		private const string SOURCE = "Jarf";

		private static Log _instance;

		/// <summary>
		/// Gets the singleton instance.
		/// </summary>
		public static Log Instance
		{
			get
			{
				if (_instance == null)
					_instance = new Log();

				return _instance;
			}
		}


		private Log()
		{
		}

		/// <summary>
		/// Writes an event to the application log.
		/// </summary>
		/// <param name="type">Type of the event.</param>
		/// <param name="text">Text of the event.</param>
		public void Write(EventLogEntryType type, string text)
		{
			// make sure the text does not exceed the maximum length of 32766 characters
			if (text.Length > 32766)
				text = text.Remove(32763) + "...";

			EventLog.WriteEntry(SOURCE, text, type);
		}
	}
}
